package com.example.lyl.yuejian.view.shop;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.lyl.yuejian.R;
import com.example.lyl.yuejian.bean.GoodsDetialBean;
import com.example.lyl.yuejian.bean.ResultBean;
import com.example.lyl.yuejian.net.NetTool;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class GoodsDetialActivity extends AppCompatActivity {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.goods_iv)
    ImageView goodsIv;
    @BindView(R.id.jj_tv)
    TextView jjTv;
    @BindView(R.id.jg_tv)
    TextView jgTv;
    @BindView(R.id.gm_btn)
    Button gmBtn;
    private int sid;
    private int uid;
    private boolean isSelect ;
    private MenuItem collect;
    private String name;
    private int price;
    private String pic;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods_detial);
        ButterKnife.bind(this);

        Intent intent = getIntent();

        sid = intent.getIntExtra("sid",0);
        uid = intent.getIntExtra("uid",0);

        Log.d("GoodsDetialActivity", "sid:" + sid);
        Log.d("GoodsDetialActivity", "uid:" + uid);


        toolbar.setTitle("商品详情");
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }

    }

    @OnClick(R.id.gm_btn)
    public void onViewClicked() {
        Intent intent = new Intent();
        intent.putExtra("uid",uid);
        intent.putExtra("sid",sid);
        intent.putExtra("name",name);
        intent.putExtra("price",price);
        intent.putExtra("pic",pic);
        intent.setClass(this,GoodsOrderActivity.class);
        startActivity(intent);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.webtoolbar,menu);
            collect = menu.findItem(R.id.collect);
            collect.setIcon(R.mipmap.shoucang0);
        NetTool.getInstance().getApi().goodsDetialRequest(uid,sid)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<GoodsDetialBean>() {
                    @Override
                    public void accept(GoodsDetialBean goodsDetialBean) throws Exception {
                        Glide.with(GoodsDetialActivity.this).load("http://"
                                +goodsDetialBean.getData().getSpic()).into(goodsIv);
                        Log.d("GoodsDetialActivity", "http://" + goodsDetialBean.getData().getSpic());
                        name = goodsDetialBean.getData().getSname();
                        price = goodsDetialBean.getData().getSprice();
                        pic = "http://" + goodsDetialBean.getData().getSpic();
                        jjTv.setText("简介:  " + goodsDetialBean.getData().getSname());
                        jgTv.setText("价格:  " + goodsDetialBean.getData().getSprice());
                        isSelect = goodsDetialBean.getData().isIsCollect();
                        Log.d("GoodsDetialActivity", "isSelect:" + isSelect);
                        if (isSelect){
                            collect.setIcon(R.mipmap.shoucang1);
                            Log.d("GoodsDetialActivity", "收藏");
                        }else {
                            collect.setIcon(R.mipmap.shoucang0);
                            Log.d("GoodsDetialActivity", "没收藏");
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {

                    }
                });
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case android.R.id.home:
                finish();
                break;
            case R.id.collect:
                if (isSelect){
                    NetTool.getInstance().getApi().goodsDelCollectRequest(uid,sid)
                            .subscribeOn(Schedulers.io())
                            .observeOn(AndroidSchedulers.mainThread())
                            .subscribe(new Consumer<ResultBean>() {
                                @Override
                                public void accept(ResultBean resultBean) throws Exception {
                                    isSelect = false;
                                    collect.setIcon(R.mipmap.shoucang0);
                                    Toast.makeText(GoodsDetialActivity.this, "取消收藏", Toast.LENGTH_SHORT).show();
                                }
                            }, new Consumer<Throwable>() {
                                @Override
                                public void accept(Throwable throwable) throws Exception {
                                    throwable.printStackTrace();
                                    Toast.makeText(GoodsDetialActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
                                }
                            });
                }else {
                    NetTool.getInstance().getApi().goodsCollectRequest(uid,sid)
                            .subscribeOn(Schedulers.io())
                            .observeOn(AndroidSchedulers.mainThread())
                            .subscribe(new Consumer<ResultBean>() {
                                @Override
                                public void accept(ResultBean resultBean) throws Exception {
                                    isSelect = true;
                                    collect.setIcon(R.mipmap.shoucang1);
                                    Toast.makeText(GoodsDetialActivity.this, "收藏成功", Toast.LENGTH_SHORT).show();
                                }
                            }, new Consumer<Throwable>() {
                                @Override
                                public void accept(Throwable throwable) throws Exception {
                                    throwable.printStackTrace();
                                    Toast.makeText(GoodsDetialActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
                                }
                            });
                }
                break;
        }
        return true;
    }
}
